package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.constant.BspsTreeNodeConstant;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.request.ComboPaymentReversalRequest;
import cc.rengu.igas.momp.facade.response.ComboPaymentReversalResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.enums.BizStatusEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * 营销合并支付异步撤销
 */
public class BspsAsynComboPaymentCancelService extends DubboAsynBaseService {

    @BIZ_PROCESS
    public void BspsAsynComboPaymentCancelService() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        rglog.debug("营销冲正异步处理服务开始,交易流水号<{}>", sysSeqNum + "M");
        /*营销合并支付订单*/
        PayOrderInfo mompPayOrderInfo = (PayOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(BspsTreeNodeConstant.MOMP_COMPAY_ORDER_INFO, PayOrderInfo.class);
        /*营销冲正订单*/
        PayOrderInfo mompCancelOrderInfo = (PayOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(BspsTreeNodeConstant.MOMP_CANCEL_ORDER_INFO, PayOrderInfo.class);
        if (null == mompPayOrderInfo || null == mompCancelOrderInfo) {
            rglog.debug("获取营销订单表异常");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        }
        ComboPaymentReversalRequest request = (ComboPaymentReversalRequest) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_REQUEST, ComboPaymentReversalRequest.class);
        if (null == request) {
            rglog.debug("请求报文异常");
            updateMompPayOrderStatus(mompPayOrderInfo, mompCancelOrderInfo, false);
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        Result<ComboPaymentReversalResponse> comboPaymentReversalResponseResult = (Result<ComboPaymentReversalResponse>) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_RESPONSE, Result.class);
        if (null == comboPaymentReversalResponseResult || null == comboPaymentReversalResponseResult.getResult()) {
            rglog.debug("响应报文异常<{}>", JSONObject.toJSONString(comboPaymentReversalResponseResult));
            updateMompPayOrderStatus(mompPayOrderInfo, mompCancelOrderInfo, false);
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ComboPaymentReversalResponse response = JSONObject.parseObject(JSONObject.toJSONString(comboPaymentReversalResponseResult.getResult()), ComboPaymentReversalResponse.class);

        /*判断冲正结果，更新状态*/
        if (comboPaymentReversalResponseResult.isSuccess() && BspsAppParamConstant.DUBBO_SUCCESS.equals(response.getRespCode())) {
            mompCancelOrderInfo.setRespDesc(response.getRespDesc());
            mompCancelOrderInfo.setRespCode(response.getRespCode());
            updateMompPayOrderStatus(mompPayOrderInfo, mompCancelOrderInfo, true);
        } else {
            mompCancelOrderInfo.setRespDesc(response.getRespDesc());
            mompCancelOrderInfo.setRespCode(response.getRespCode());
            updateMompPayOrderStatus(mompPayOrderInfo, mompCancelOrderInfo, false);
        }
    }

    /**
     * 更改营销订单状态
     *
     * @param bStatus boolean 撤销结果
     * @throws Exception 异常
     */
    private void updateMompPayOrderStatus(PayOrderInfo mompPayOrderInfo, PayOrderInfo mompCancelOrderInfo, boolean bStatus) throws Exception {
        if (bStatus) {
            mompPayOrderInfo.setBizStatus(BizStatusEnum.REVSAL.getStatus());
            mompCancelOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
            mompCancelOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
        } else {
            mompPayOrderInfo.setBizStatus(BizStatusEnum.REVSAL.getStatus());
            mompCancelOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            mompCancelOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
        }
        OrderService orderService = new OrderServiceImpl();

        boolean bResult = orderService.updateOrderInfo(null, null, mompPayOrderInfo);
        boolean bCancelOrderResult = orderService.updateOrderInfo(null, null, mompCancelOrderInfo);
        if (!bResult || !bCancelOrderResult) {
            rglog.error("更新营销订单失败.");
            throw new BizException(RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespDesc());
        }
        rglog.debug("更新营销订单成功.");
    }
}
